package com.moudle.app.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.support.v4.app.FragmentManager;

import com.moudle.app.R;
import com.moudle.app.base.BaseFragMent;
import com.moudle.app.base.BaseActivity;
import com.moudle.app.AppException;
import com.moudle.app.bean.Entity;
import com.moudle.app.common.StringUtils;
import com.moudle.app.common.UIHelper;
import com.moudle.app.ui.LoginActivity;

import java.io.Serializable;


/**
 * @Description 忘记密码模块
 * @Author Li Chao
 * @Date 2015/12/10 11:23
 */
public class ForgetPwdFragment extends BaseFragMent {
    private TextView mTvTop;// 标题字
    private EditText mEtUsername;// 手机号
    private EditText mEtLoginCode;// 密码
    private EditText mEtVcode;// 验证码
    private Button mBtnGetCode;// 获取修改码
    private Button mBtnModify;// 密码重设按钮
    private String mUsername;
    private String mPassword;
    private String mCode;
    private ImageButton mBtnBack;
    private InputMethodManager mImm;
    private AlertDialog mAlertDlg = null;
    private FragmentManager fm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View v = inflater.inflate(R.layout.fragment_forget_pwd, null);
        mAlertDlg = new AlertDialog.Builder(getActivity()).create();
        fm = getActivity().getSupportFragmentManager();
        mImm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        initView(v);
        initData();
        return v;
    }

    public void initView(View v) {
       // mTvTop = (TextView) v.findViewById(R.id.acitivity_name);
        mTvTop.setText(R.string.edit_pad);// 设置Head字体显示
        mEtUsername = (EditText) v.findViewById(R.id.login_username);
        mEtLoginCode = (EditText) v.findViewById(R.id.login_password);
        mEtVcode = (EditText) v.findViewById(R.id.v_code);
        mBtnGetCode = (Button) v.findViewById(R.id.get_code);
        mBtnModify = (Button) v.findViewById(R.id.btn_modify);
       // mBtnBack = (ImageButton) v.findViewById(R.id.btn_back);
        // 打开软键盘
        mImm.showSoftInput(mEtUsername, InputMethodManager.RESULT_SHOWN);
        mImm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
        mBtnGetCode.setOnClickListener(resetListen);
        mBtnModify.setOnClickListener(resetListen);
        mBtnBack.setOnClickListener(resetListen);
    }

    private View.OnClickListener resetListen = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.get_code:
                    mUsername = mEtUsername.getText().toString().trim();
                    mPassword = mEtLoginCode.getText().toString().trim();
                    if (StringUtils.isEmpty(mUsername)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtUsername, R.string.login_phone_prompt);
                        return;
                    }
                    if (!StringUtils.isPhoneNum(mUsername)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtUsername, R.string.login_correct_phone);
                        return;
                    }
                    if (StringUtils.isEmpty(mPassword)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtLoginCode, R.string.login_password_prompt);
                        return;
                    }
                    mAlertDlg.show();
                    UIHelper.showLoadingDialog(mAlertDlg, getActivity(), R.string.get_code);
                    new ForgetPwdNetworkAsyn(LoginActivity.REQUEST_CODE).execute(mUsername);
                    break;
                case R.id.btn_modify:
                    mCode = mEtVcode.getText().toString().trim();
                    if (StringUtils.isEmpty(mCode)) {
                        ((BaseActivity) getActivity()).errorPrompt(mEtVcode, R.string.register_verifycode_prompt);
                        return;
                    }
                    mAlertDlg.show();
                    UIHelper.showLoadingDialog(mAlertDlg, getActivity(), R.string.edit_pad);
                    new ForgetPwdNetworkAsyn(LoginActivity.REQUEST_FORGET_PWD).execute(mUsername, mPassword, mCode);
                    break;
            }
        }
    };

    private class ForgetPwdNetworkAsyn extends AsyncTask<String, Void, Serializable> {
        private int mDataFlag;

        public ForgetPwdNetworkAsyn(int dataFlag) {
            this.mDataFlag = dataFlag;
        }

        @Override
        protected Serializable doInBackground(String... params) {
            Serializable serializable = null;
            try {
                switch (mDataFlag) {
                    case LoginActivity.REQUEST_CODE:
                        serializable = getApplication().getCode(params[0]);
                        break;
                    case LoginActivity.REQUEST_FORGET_PWD:
                        serializable = getApplication().resetPwd(params[0], params[1], params[2]);
                        break;
                }
            } catch (AppException e) {
                AppException.run(e);
            }
            return serializable;
        }

        @Override
        protected void onPostExecute(Serializable result) {
            mAlertDlg.dismiss();
            if (result != null) {
                switch (mDataFlag) {
                    case LoginActivity.REQUEST_CODE:
                        UIHelper.ToastMessage(getActivity(), ((Entity) result).message);
                        break;
                    case LoginActivity.REQUEST_FORGET_PWD:
                        if (((Entity) result).isStatus()) {
                            fm.beginTransaction().setCustomAnimations(R.anim.slide_left_enter, R.anim.slide_right_exit).remove(ForgetPwdFragment.this).commit();
                            fm.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        }
                        UIHelper.ToastMessage(getActivity(), ((Entity) result).message);
                        break;
                }
            } else {
                executeOnLoadDataError();
            }
        }
    }

    private void executeOnLoadDataError() {
        AppException.getAppExceptionHandler().makeToast(getActivity());
    }
}
